rm(list=ls())
library(vegan)
library(psych)##
library(igraph)
library(beepr)
setwd("/Users/chenggao/Google_Drive/EPICON.NC.2022")

load("Bac.Fung.data.prep.stopby8datasets.amfL.RSLZxBF.Rarefaction.2021.08.13.rdata")
BF0<-read.csv("Microbiome/0000BacteriaFungi.1029x1293.13165.2019.07.10.csv", head = T, row.names = 1)
ID.tmp<-BF0[,c("Kingdom",   "Kingdom1", "Phylum",   "Class",    "Order",    "Family",   "Genus",    "Funguild", "OTU.ID",   "Morph",    "Morph1")]

fq <- 7; abu <- 19; 

SpMan<-function(fq, abu, treatment, tpA, tpB){
  
  d1 <- d1.raw [env.R$Treatment1==treatment & env.R$TP > tpA & env.R$TP < tpB , ]
  d1 <- d1 [,specnumber(t(d1)) > fq & colSums(d1)> abu]
  d2 <- d2.raw [env.R$Treatment1==treatment & env.R$TP > tpA & env.R$TP < tpB, ]
  d2 <- d2[, specnumber(t(d2)) > fq & colSums(d2)> abu]
  env.R.tmp0 <- env.R [env.R$Treatment1==treatment & env.R$TP > tpA & env.R$TP < tpB , ]
  
  d3 <- d3.raw [env.Z$Treatment1==treatment & env.Z$TP > tpA & env.Z$TP < tpB , ]
  d3 <- d3 [,specnumber(t(d3)) > fq & colSums(d3)> abu]
  d4 <- d4.raw [env.Z$Treatment1==treatment & env.Z$TP > tpA & env.Z$TP < tpB , ]
  d4 <- d4 [,specnumber(t(d4)) > fq & colSums(d4)> abu]
  env.Z.tmp0 <- env.Z [env.Z$Treatment1==treatment & env.Z$TP > tpA & env.Z$TP < tpB , ]
  
  d5 <- d5.raw [env.S$Treatment1==treatment & env.S$TP > tpA & env.S$TP < tpB, ]
  d5 <- d5[, specnumber(t(d5)) > fq & colSums(d5)> abu]
  d6 <- d6.raw [env.S$Treatment1==treatment & env.S$TP > tpA & env.S$TP < tpB , ]
  d6 <- d6 [,specnumber(t(d6)) > fq & colSums(d6)> abu]
  env.S.tmp0 <- env.S [env.S$Treatment1==treatment & env.S$TP > tpA & env.S$TP < tpB , ]
  
  d7 <- d7.raw [env.L$Treatment1==treatment & env.L$TP > tpA & env.L$TP < tpB , ]
  d7 <- d7[,specnumber(t(d7)) > fq & colSums(d7)> abu]
  d8 <- d8.raw [env.L$Treatment1==treatment & env.L$TP > tpA & env.L$TP < tpB, ]
  d8 <- d8[, specnumber(t(d8)) > fq & colSums(d8)> abu]
  env.L.tmp0 <- env.L [env.L$Treatment1==treatment & env.L$TP > tpA & env.L$TP < tpB , ]
  
  saveRDS(env.L.tmp0, paste0("Fig.3/BFT.SpMan.sig.df.FDR.padj.2022.05.31/data.env.Leaf.", treatment,".",tpA,".",tpB,".",fq,".",abu,".RDS"))
  saveRDS(env.S.tmp0, paste0("Fig.3/BFT.SpMan.sig.df.FDR.padj.2022.05.31/data.env.Soil.", treatment,".",tpA,".",tpB,".",fq,".",abu,".RDS"))
  saveRDS(env.Z.tmp0, paste0("Fig.3/BFT.SpMan.sig.df.FDR.padj.2022.05.31/data.env.Rhizosphere.", treatment,".",tpA,".",tpB,".",fq,".",abu,".RDS"))
  saveRDS(env.R.tmp0, paste0("Fig.3/BFT.SpMan.sig.df.FDR.padj.2022.05.31/data.env.Root.", treatment,".",tpA,".",tpB,".",fq,".",abu,".RDS"))
  
  saveRDS(d1, paste0("Fig.3/BFT.SpMan.sig.df.FDR.padj.2022.05.31/data.bac.Root.", treatment,".",tpA,".",tpB,".",fq,".",abu,".RDS"))
  saveRDS(d2, paste0("Fig.3/BFT.SpMan.sig.df.FDR.padj.2022.05.31/data.fung.Root.", treatment,".",tpA,".",tpB,".",fq,".",abu,".RDS"))
  
  saveRDS(d3, paste0("Fig.3/BFT.SpMan.sig.df.FDR.padj.2022.05.31/data.bac.Rhizosphere.", treatment,".",tpA,".",tpB,".",fq,".",abu,".RDS"))
  saveRDS(d4, paste0("Fig.3/BFT.SpMan.sig.df.FDR.padj.2022.05.31/data.fung.Rhizosphere.", treatment,".",tpA,".",tpB,".",fq,".",abu,".RDS"))
  
  saveRDS(d5, paste0("Fig.3/BFT.SpMan.sig.df.FDR.padj.2022.05.31/data.bac.Soil.", treatment,".",tpA,".",tpB,".",fq,".",abu,".RDS"))
  saveRDS(d6, paste0("Fig.3/BFT.SpMan.sig.df.FDR.padj.2022.05.31/data.fung.Soil.", treatment,".",tpA,".",tpB,".",fq,".",abu,".RDS"))
  
  saveRDS(d7, paste0("Fig.3/BFT.SpMan.sig.df.FDR.padj.2022.05.31/data.bac.Leaf.", treatment,".",tpA,".",tpB,".",fq,".",abu,".RDS"))
  saveRDS(d8, paste0("Fig.3/BFT.SpMan.sig.df.FDR.padj.2022.05.31/data.fung.Leaf.", treatment,".",tpA,".",tpB,".",fq,".",abu,".RDS"))
  d12<-cbind(d1,d2)
  spman.d12 = corr.test(d12, use="pairwise",method="spearman",adjust="fdr", alpha=.05, ci=FALSE)
  saveRDS(spman.d12, paste0("Fig.3/BFT.SpMan.sig.df.FDR.padj.2022.05.31/SpMan.crossBF.Root.", treatment,".",tpA,".",tpB,".",fq,".",abu,".RDS"))

  d34<-cbind(d3,d4)
  spman.d34 = corr.test(d34, use="pairwise",method="spearman",adjust="fdr", alpha=.05, ci=FALSE)
  saveRDS(spman.d34, paste0("Fig.3/BFT.SpMan.sig.df.FDR.padj.2022.05.31/SpMan.crossBF.Rhizosphere.", treatment,".",tpA,".",tpB,".",fq,".",abu,".RDS"))
  
  d56<-cbind(d5,d6)
  spman.d56 = corr.test(d56, use="pairwise",method="spearman",adjust="fdr", alpha=.05, ci=FALSE)
  saveRDS(spman.d56, paste0("Fig.3/BFT.SpMan.sig.df.FDR.padj.2022.05.31/SpMan.crossBF.Soil.", treatment,".",tpA,".",tpB,".",fq,".",abu,".RDS"))
  
  d78<-cbind(d7,d8)
  spman.d78 = corr.test(d78, use="pairwise",method="spearman",adjust="fdr", alpha=.05, ci=FALSE)
  saveRDS(spman.d78, paste0("Fig.3/BFT.SpMan.sig.df.FDR.padj.2022.05.31/SpMan.crossBF.Leaf.", treatment,".",tpA,".",tpB,".",fq,".",abu,".RDS"))
beep()
}

#SpMan(fq, abu, "Control", 2, 9)
#SpMan(fq, abu, "Pre_flowering_drought", 2, 9)
#SpMan(fq, abu, "Control", 8, 18)
#SpMan(fq, abu, "Pre_flowering_drought", 8, 18)
#SpMan(fq, abu, "Control", 9, 18)
#SpMan(fq, abu, "Post_flowering_drought", 9, 18)

######################################
# significant positive correlations #
#####################################
setwd("/Users/chenggao/Google_Drive/EPICON.NC.2022")
BF0<-read.csv("Microbiome/0000BacteriaFungi.1029x1293.13165.2019.07.10.csv", head = T, row.names = 1)
ID.tmp<-BF0[,c("Kingdom",   "Kingdom1", "Phylum",   "Class",    "Order",    "Family",   "Genus",    "Funguild", "OTU.ID",   "Morph",    "Morph1")]

r.cutoff = 0.6
p.cutoff = 0.05

SpMan.Rsig<-function(fq, abu, habitat, treatment, tpA, tpB){
  d1 <- readRDS(paste0("Fig.3/BFT.SpMan.sig.df.FDR.padj.2022.05.31/data.bac.",habitat,".", treatment,".",tpA,".",tpB,".",fq,".",abu,".RDS"))
  d2 <- readRDS(paste0("Fig.3/BFT.SpMan.sig.df.FDR.padj.2022.05.31/data.fung.",habitat,".", treatment,".",tpA,".",tpB,".",fq,".",abu,".RDS"))
  d12 <- data.frame(d1, d2)
  
  spman.r0 <- readRDS(paste0("Fig.3/BFT.SpMan.sig.df.FDR.padj.2022.05.31/SpMan.crossBF.",habitat,".", treatment,".",tpA,".",tpB,".",fq,".",abu,".RDS"))

  Cor<-as.matrix(spman.r0$r)
  Cor.df<-data.frame(row=rownames(Cor)[row(Cor)[upper.tri(Cor)]], 
                     col=colnames(Cor)[col(Cor)[upper.tri(Cor)]], Cor=Cor[upper.tri(Cor)])
  
  P0<-as.matrix(spman.r0$p)
  P.df<-data.frame(row=rownames(P0)[row(P0)[upper.tri(P0)]], 
                     col=colnames(P0)[col(P0)[upper.tri(P0)]], p=P0[upper.tri(P0)])
  
  df <- data.frame(Cor.df,  P.df, Habitat = habitat, Treatment = treatment, TPA = tpA)
  da.tmp<-df.sig<- df[ df$Cor > r.cutoff & df$p < p.cutoff,] 
  V1<-data.frame("v1"=da.tmp$row); V2<-data.frame("v2"=da.tmp$col)
  IDsub1<-ID.tmp[ID.tmp$OTU.ID %in% V1$v1, ]; IDsub2<-ID.tmp[ID.tmp$OTU.ID %in% V2$v2, ]
  V1$id  <- 1:nrow(V1); V2$id  <- 1:nrow(V2)
  M1<-merge(V1, IDsub1, by.x = "v1", by.y = "OTU.ID", all.x= T); M1<-M1[order(M1$id), ]
  M2<-merge(V2, IDsub2, by.x = "v2", by.y = "OTU.ID", all.x = T); M2<-M2[order(M2$id), ]
  df.tmp<-data.frame(da.tmp, M1, M2)
  saveRDS(df.tmp, paste0("Fig.3/BFT.SpMan.sig.df.2022.05.31/SpMan.Rsig.Bac-Fung.",habitat,".", treatment,".",tpA,".",tpB,".",fq,".",abu,".RDS"))
}

#SpMan.Rsig(fq, abu,"Root", "Control", 2, 9)
#SpMan.Rsig(fq, abu,"Root", "Pre_flowering_drought", 2, 9)
#SpMan.Rsig(fq, abu,"Root", "Control", 8, 18)
#SpMan.Rsig(fq, abu,"Root", "Pre_flowering_drought", 8, 18)

#SpMan.Rsig(fq, abu,"Rhizosphere", "Control", 2, 9)
#SpMan.Rsig(fq, abu,"Rhizosphere", "Pre_flowering_drought", 2, 9)
#SpMan.Rsig(fq, abu,"Rhizosphere", "Control", 8, 18)
#SpMan.Rsig(fq, abu,"Rhizosphere", "Pre_flowering_drought", 8, 18)

#SpMan.Rsig(fq, abu,"Soil", "Control", 2, 9)
#SpMan.Rsig(fq, abu,"Soil", "Pre_flowering_drought", 2, 9)
#SpMan.Rsig(fq, abu,"Soil", "Control", 8, 18)
#SpMan.Rsig(fq, abu,"Soil", "Pre_flowering_drought", 8, 18)

#SpMan.Rsig(fq, abu,"Leaf", "Control", 2, 9)
#SpMan.Rsig(fq, abu,"Leaf", "Pre_flowering_drought", 2, 9)
#SpMan.Rsig(fq, abu,"Leaf", "Control", 8, 18)
#SpMan.Rsig(fq, abu,"Leaf", "Pre_flowering_drought", 8, 18)

library(vegan)
library(psych)##
library(igraph)
library(beepr)
library(igraph)

setwd("/Users/chenggao/Google_Drive/EPICON.NC.2022")
load("Bac.Fung.data.prep.stopby8datasets.amfL.RSLZxBF.Rarefaction.2021.08.13.rdata")

####################
# Module cross BF###
####################

bac.fung.cross.module.net<-function(type,fq, abu, habitat, treatment, tpA, tpB){
  BF0<-read.csv("Microbiome/0000BacteriaFungi.1029x1293.13165.2019.07.10.csv", head = T, row.names = 1)
  ID.tmp<-BF0[,c("Kingdom", "Kingdom1", "Phylum",   "Class",    "Order",    "Family",   "Genus",    "Funguild", "OTU.ID",   "Morph",    "Morph1")]
  da<-readRDS(paste0("Fig.3/BFT.",type,".sig.df.2022.05.31/",type,".Rsig.Bac-Fung.",habitat,".", treatment,".",tpA,".",tpB,".",fq,".",abu,".RDS"))
  g <- graph.data.frame(da, directed=FALSE)
  
  fun.fc<-cluster_fast_greedy(g)
  print(modularity(fun.fc))
  modularity(fun.fc,membership(fun.fc))
  membership(fun.fc)
  print(sizes(fun.fc))###
  fun.comps <- membership(fun.fc)
  colbar <-c("#0000FF","#FF3399","#FFCC33","#9999FF", "#FF99FF", "#FFFFCC",
             "#33FFFF","#CCFF00","#CCFFCC","#FF3300","#CC99CC","#CC66CC","#FFFFCC","#CC0033","#666666","slateblue1","springgreen","steelblue1","tan1","thistle1","tomato","turquoise", "violet", "red", "yellowgreen","peachpuff", "peru", "pink", "plum2", "purple","wheat", "cornsilk3","cornsilk","coral4","coral",
             "chocolate4","chocolate","black","chartreuse4","chartreuse","burlywood4","burlywood","brown4","blue","bisque4","bisque",
             "azure4","azure","aquamarine4","aquamarine","antiquewhite4","antiquewhite","aliceblue","dodgerblue4","dodgerblue","dimgrey",
             "deepskyblue4", "deepskyblue", "deeppink4", "deeppink","darkviolet", "darkslategray4","darkslategray",
             "darkseagreen4", "darkseagreen", "darksalmon", "darkred", "darkorchid4", "darkorchid", "darkorange4",
             "darkorange","firebrick","darkgreen", "darkgoldenrod4","grey", "darkgoldenrod", "darkcyan")
  V(g)$color <- colbar[fun.comps]
    set.seed(123)
  plot(g, edge.width=0.5,edge.color="grey", vertex.frame.color=NA,vertex.label=NA,edge.lty=1,     edge.curved=T,vertex.size=3,margin=c(0, 0,0,0)) 
}

par(mfrow=c(4,4),mar=c(0, 0, 0, 0))
bac.fung.cross.module.net("SpMan",fq, abu,"Root","Control", 2, 9)
## [1] 0.5190281
## Community sizes
##   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18 
## 223 332 113  17   2   3   2   2   2   2   2   2   2   2   2   2   2   2
bac.fung.cross.module.net("SpMan",fq, abu,"Root","Pre_flowering_drought", 2, 9)
## [1] 0.8119668
## Community sizes
##  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 
## 12 15  6  4  4  6  3  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2 
## 27 28 29 30 31 32 33 
##  2  2  2  2  2  2  2
bac.fung.cross.module.net("SpMan",fq, abu,"Root","Control", 8, 18)
## [1] 0.4485985
## Community sizes
##   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20 
## 126 180  41  13   9   5   6   5   4   3   3   2   2   2   2   2   2   2   2   2 
##  21  22  23  24  25  26  27  28  29  30 
##   2   2   2   2   2   2   2   2   2   2
bac.fung.cross.module.net("SpMan",fq, abu,"Root","Pre_flowering_drought", 8, 18)
## [1] 0.3723379
## Community sizes
##   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20 
## 285 183  86   7   3   2   2   2   3   2   3   2   2   2   2   2   2   2   2   2
bac.fung.cross.module.net("SpMan",fq, abu,"Rhizosphere","Control", 2, 9)
## [1] 0.4582705
## Community sizes
##   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20 
## 189 308 359  16  10   9   5   7   5   6  32   2   2   3   3   3   4   3   3   3 
##  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40 
##   3   3   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2 
##  41  42  43  44  45  46  47  48  49  50  51 
##   2   2   2   2   2   2   2   2   2   2   2
bac.fung.cross.module.net("SpMan",fq, abu,"Rhizosphere","Pre_flowering_drought", 2, 9)
## [1] 0.6884142
## Community sizes
##  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 
## 34 59 76 24 17 26 10 12  6  5  3  5  5  4  4  4  3  3  3  3  3  3  3  3  3  3 
## 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 
##  3  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2 
## 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 
##  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2 
## 79 80 81 82 83 
##  2  2  2  2  2
bac.fung.cross.module.net("SpMan",fq, abu,"Rhizosphere","Control", 8, 18)
## [1] 0.4815539
## Community sizes
##   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20 
## 279 356 104  20   8  11   4   5   4   4   3   3   2   4   2   3   3   3   3   3 
##  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40 
##   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2 
##  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56 
##   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2
bac.fung.cross.module.net("SpMan",fq, abu,"Rhizosphere","Pre_flowering_drought", 8, 18)
## [1] 0.32127
## Community sizes
##   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20 
## 219 305 260  55   4   3   6   2   3   2   3   2   2   2   2   2   2   2   2   2 
##  21  22  23  24  25  26  27 
##   2   2   2   2   2   2   2
bac.fung.cross.module.net("SpMan",fq, abu,"Soil","Control", 2, 9)
## [1] 0.6840684
## Community sizes
##   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20 
## 176  91  30 117  29  19  11   8   7   6   6   5   5   5   5   5   4   4   6   4 
##  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40 
##   3   3   3   4   4   3   3   3   3   3   3   3   3   3   3   3   3   2   2   2 
##  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60 
##   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2 
##  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80 
##   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2 
##  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 
##   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2 
## 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 
##   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2 
## 121 122 123 124 125 126 127 128 129 130 131 132 133 
##   2   2   2   2   2   2   2   2   2   2   2   2   2
bac.fung.cross.module.net("SpMan",fq, abu,"Soil","Pre_flowering_drought", 2, 9)
## [1] 0.8380596
## Community sizes
##  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 
## 36 32 12 14  7  6  5  7  4  4  4  4  4  3  3  3  3  4  3  3  3  3  3  3  3  3 
## 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 
##  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2 
## 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 
##  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2
bac.fung.cross.module.net("SpMan",fq, abu,"Soil","Control", 8, 18)
## [1] 0.7882476
## Community sizes
##   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20 
## 129 114 109  32  31  31  22  19  13  10   7   6   6  11   8   5   4   4   4   8 
##  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40 
##   5   5   4   3   3   3   3   3   3   3   3   3   3   3   2   2   2   2   2   2 
##  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60 
##   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2 
##  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80 
##   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2 
##  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 
##   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2 
## 101 102 103 104 
##   2   2   2   2
bac.fung.cross.module.net("SpMan",fq, abu,"Soil","Pre_flowering_drought", 8, 18)
## [1] 0.6932747
## Community sizes
##   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20 
## 177 157 119  40  27  20  12   6   7   7   6   5   3   3   3   3   3   3   3   3 
##  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40 
##   3   3   3   3   3   3   3   3   3   3   2   2   2   2   2   2   2   2   2   2 
##  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60 
##   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2 
##  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80 
##   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2 
##  81  82  83  84  85  86 
##   2   2   2   2   2   2
bac.fung.cross.module.net("SpMan",fq, abu,"Leaf","Control", 2, 9)
## [1] 0.7427912
## Community sizes
##  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 
## 12  8  6 15  6  4  3  3  3  3  2  2  2  2  2  2  2
bac.fung.cross.module.net("SpMan",fq, abu,"Leaf","Pre_flowering_drought", 2, 9)
## [1] 0.6853
## Community sizes
##  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 
## 23 15 17 16 10  7  4  4  7  3  3  3  2  2  2
bac.fung.cross.module.net("SpMan",fq, abu,"Leaf","Control", 8, 18)
## [1] 0.4014326
## Community sizes
##  1  2  3  4  5  6  7  8  9 10 11 
## 43 18 20  5  5  5  8  2  2  2  2
bac.fung.cross.module.net("SpMan",fq, abu,"Leaf","Pre_flowering_drought", 8, 18)
## [1] 0.3305583
## Community sizes
##  1  2  3  4  5  6  7  8  9 10 11 12 
## 51 55 58 12  2  2  2  2  2  2  2  2

#####################
# Module fung-fung###
####################

fung.module.net<-function(type,fq, abu, habitat, treatment, tpA, tpB){
  BF0<-read.csv("Microbiome/0000BacteriaFungi.1029x1293.13165.2019.07.10.csv", head = T, row.names = 1)
  ID.tmp<-BF0[,c("Kingdom", "Kingdom1", "Phylum",   "Class",    "Order",    "Family",   "Genus",    "Funguild", "OTU.ID",   "Morph",    "Morph1")]
  da0<-readRDS(paste0("Fig.3/BFT.",type,".sig.df.2022.05.31/",type,".Rsig.Bac-Fung.",habitat,".", treatment,".",tpA,".",tpB,".",fq,".",abu,".RDS"))
  da <- da0[da0$Kingdom == "Eukaryote" & da0$Kingdom.1 == "Eukaryote",]
  g <- graph.data.frame(da, directed=FALSE)
  fun.fc<-cluster_fast_greedy(g)
  print(modularity(fun.fc))
  modularity(fun.fc,membership(fun.fc))
  membership(fun.fc)
  print(sizes(fun.fc))###
  fun.comps <- membership(fun.fc)
  colbar <-c("#0000FF","#FF3399","#FFCC33","#9999FF", "#FF99FF", "#FFFFCC",
             "#33FFFF","#CCFF00","#CCFFCC","#FF3300","#CC99CC","#CC66CC","#FFFFCC","#CC0033","#666666","slateblue1","springgreen","steelblue1","tan1","thistle1","tomato","turquoise", "violet", "red", "yellowgreen", "peachpuff", "peru", "pink", "plum2", "purple","wheat", "cornsilk3","cornsilk","coral4","coral",
             "chocolate4","chocolate","black","chartreuse4","chartreuse","burlywood4","burlywood","brown4","blue","bisque4","bisque",
             "azure4","azure","aquamarine4","aquamarine","antiquewhite4","antiquewhite","aliceblue","dodgerblue4","dodgerblue","dimgrey",
             "deepskyblue4", "deepskyblue", "deeppink4", "deeppink","darkviolet", "darkslategray4","darkslategray",
             "darkseagreen4", "darkseagreen", "darksalmon", "darkred", "darkorchid4", "darkorchid", "darkorange4",
             "darkorange","firebrick","darkgreen", "darkgoldenrod4","grey", "darkgoldenrod", "darkcyan")
  V(g)$color <- colbar[fun.comps]
    set.seed(123)
  plot(g, edge.width=0.5,edge.color="grey", vertex.frame.color=NA,vertex.label=NA,edge.lty=1,     edge.curved=T,vertex.size=3,margin=c(0, 0,0,0)) 
}
par(mfrow=c(4,4),mar=c(0, 0, 0, 0))
fung.module.net("SpMan",fq, abu,"Root","Control", 2, 9)
## [1] 0.346367
## Community sizes
##  1  2  3  4  5  6  7  8  9 
## 22 28 24  7  2  2  2  2  2
fung.module.net("SpMan",fq, abu,"Root","Pre_flowering_drought", 2, 9)
## [1] 0.75
## Community sizes
## 1 2 3 4 5 
## 4 3 2 2 2
fung.module.net("SpMan",fq, abu,"Root","Control", 8, 18)
## [1] 0.7318325
## Community sizes
##  1  2  3  4  5  6  7  8  9 10 11 12 
##  8  7  5 16 12  3  3  2  2  2  2  2
fung.module.net("SpMan",fq, abu,"Root","Pre_flowering_drought", 8, 18)
## [1] 0.5361161
## Community sizes
##  1  2  3  4  5  6  7  8  9 10 11 12 13 14 
##  9  6 12 11  3  9  4  3  2  2  2  2  2  2
fung.module.net("SpMan",fq, abu,"Rhizosphere","Control", 2, 9)
## [1] 0.6985941
## Community sizes
##  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 
## 17  8  7  6  4  9  4  3  3  2  2  2  2  2  2  2  2
fung.module.net("SpMan",fq, abu,"Rhizosphere","Pre_flowering_drought", 2, 9)
## [1] 0.4827387
## Community sizes
##  1  2  3  4  5  6  7  8  9 10 11 12 13 14 
## 19 19 14  5  2  5  3  3  3  2  2  2  2  2
fung.module.net("SpMan",fq, abu,"Rhizosphere","Control", 8, 18)
## [1] 0.3900327
## Community sizes
##  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 
## 67 28 11 26  4  2  4  3  2  2  2  2  2  2  2
fung.module.net("SpMan",fq, abu,"Rhizosphere","Pre_flowering_drought", 8, 18)
## [1] 0.5292602
## Community sizes
##  1  2  3  4  5  6  7  8  9 10 11 12 13 14 
## 39 27  4  3  3  2  2  2  2  2  2  2  2  2
fung.module.net("SpMan",fq, abu,"Soil","Control", 2, 9)
## [1] 0.6766205
## Community sizes
##  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 
## 15  7  5  4  9  6  3  3  2  2  2  2  2  2  2  2  2  2  2
fung.module.net("SpMan",fq, abu,"Soil","Pre_flowering_drought", 2, 9)
## [1] 0.78738
## Community sizes
##  1  2  3  4  5  6  7  8  9 10 11 12 
##  5  5  4  3  3  2  2  2  2  2  2  2
fung.module.net("SpMan",fq, abu,"Soil","Control", 8, 18)
## [1] 0.8655266
## Community sizes
##  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 
## 22 15  6  5 15  8  4  5  5  4  3  3  3  3  3  3  2  2  2  2  2  2  2  2  2  2 
## 27 28 
##  2  2
fung.module.net("SpMan",fq, abu,"Soil","Pre_flowering_drought", 8, 18)
## [1] 0.8380582
## Community sizes
##  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 
##  8  6 10  6  5  4  3  3  3  3  2  2  2  2  2  2  2  2  2  2  2  2
fung.module.net("SpMan",fq, abu,"Leaf","Control", 2, 9)
## [1] 0.278699
## Community sizes
##  1  2  3  4  5 
## 11  6  4  2  2
fung.module.net("SpMan",fq, abu,"Leaf","Pre_flowering_drought", 2, 9)
## [1] 0.3234711
## Community sizes
## 1 2 3 4 
## 9 5 3 7
fung.module.net("SpMan",fq, abu,"Leaf","Control", 8, 18)
## [1] 0.6061801
## Community sizes
##  1  2  3  4 
## 11  8  5  2
fung.module.net("SpMan",fq, abu,"Leaf","Pre_flowering_drought", 8, 18)
## [1] 0.518338
## Community sizes
##  1  2  3  4  5  6  7  8 
## 13 16  5  3  3  2  2  2

####################
# Module bac-bac###
####################

bac.module.net<-function(type,fq, abu, habitat, treatment, tpA, tpB){
  BF0<-read.csv("Microbiome/0000BacteriaFungi.1029x1293.13165.2019.07.10.csv", head = T, row.names = 1)
  ID.tmp<-BF0[,c("Kingdom", "Kingdom1", "Phylum",   "Class",    "Order",    "Family",   "Genus",    "Funguild", "OTU.ID",   "Morph",    "Morph1")]
  da0<-readRDS(paste0("Fig.3/BFT.",type,".sig.df.2022.05.31/",type,".Rsig.Bac-Fung.",habitat,".", treatment,".",tpA,".",tpB,".",fq,".",abu,".RDS"))
  da <- da0[da0$Kingdom == "Prokaryote" & da0$Kingdom.1 == "Prokaryote",]
  g <- graph.data.frame(da, directed=FALSE)
  
  fun.fc<-cluster_fast_greedy(g)
  print(modularity(fun.fc))
  modularity(fun.fc,membership(fun.fc))
  membership(fun.fc)
  print(sizes(fun.fc))###
  fun.comps <- membership(fun.fc)
  colbar <-c("#0000FF","#FF3399","#FFCC33","#9999FF", "#FF99FF", "#FFFFCC",
             "#33FFFF","#CCFF00","#CCFFCC","#FF3300","#CC99CC","#CC66CC","#FFFFCC","#CC0033","#666666","slateblue1","springgreen","steelblue1","tan1","thistle1","tomato","turquoise", "violet", "red", "yellowgreen",    "peachpuff", "peru", "pink", "plum2", "purple","wheat", "cornsilk3","cornsilk","coral4","coral",
             "chocolate4","chocolate","black","chartreuse4","chartreuse","burlywood4","burlywood","brown4","blue","bisque4","bisque",
             "azure4","azure","aquamarine4","aquamarine","antiquewhite4","antiquewhite","aliceblue","dodgerblue4","dodgerblue","dimgrey",
             "deepskyblue4", "deepskyblue", "deeppink4", "deeppink","darkviolet", "darkslategray4","darkslategray",
             "darkseagreen4", "darkseagreen", "darksalmon", "darkred", "darkorchid4", "darkorchid", "darkorange4",
             "darkorange","firebrick","darkgreen", "darkgoldenrod4","grey", "darkgoldenrod", "darkcyan")
  V(g)$color <- colbar[fun.comps]
  
  set.seed(123)
  plot(g, edge.width=0.5,edge.color="grey", vertex.frame.color=NA,vertex.label=NA,edge.lty=1,     edge.curved=T,vertex.size=3,margin=c(0, 0,0,0)) 
}
par(mfrow=c(4,4),mar=c(0, 0, 0, 0))
bac.module.net("SpMan",fq, abu,"Root","Control", 2, 9)
## [1] 0.4797443
## Community sizes
##   1   2   3   4   5   6   7   8   9  10 
## 113 202 270  10   3   2   2   2   2   2
bac.module.net("SpMan",fq, abu,"Root","Pre_flowering_drought", 2, 9)
## [1] 0.7947377
## Community sizes
##  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 
## 10  6 12  4  6  3  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2
bac.module.net("SpMan",fq, abu,"Root","Control", 8, 18)
## [1] 0.4030565
## Community sizes
##   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20 
##  97 176   8   7   9   6   3   3   4   3   3   2   3   2   2   2   2   2   2   2 
##  21  22  23  24  25 
##   2   2   2   2   2
bac.module.net("SpMan",fq, abu,"Root","Pre_flowering_drought", 8, 18)
## [1] 0.3857577
## Community sizes
##   1   2   3   4   5   6   7   8   9  10  11  12 
## 247 159  69   2   2   3   3   2   2   2   2   2
bac.module.net("SpMan",fq, abu,"Rhizosphere","Control", 2, 9)
## [1] 0.4598438
## Community sizes
##   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20 
## 162 342 283  14   9   8   5   7   2   2   2   3   2   3   2   3   2   2   2   2 
##  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35 
##   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2
bac.module.net("SpMan",fq, abu,"Rhizosphere","Pre_flowering_drought", 2, 9)
## [1] 0.7244696
## Community sizes
##  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 
## 24 47 51 18 17 11  5  5  7  3  4  3  4  3  3  3  3  3  2  2  2  2  2  2  2  2 
## 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 
##  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2 
## 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 
##  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2
bac.module.net("SpMan",fq, abu,"Rhizosphere","Control", 8, 18)
## [1] 0.4066005
## Community sizes
##   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20 
## 259 101 202  31  17   4   4   5   4   3   3   2   2   2   3   3   3   2   2   2 
##  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36 
##   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2
bac.module.net("SpMan",fq, abu,"Rhizosphere","Pre_flowering_drought", 8, 18)
## [1] 0.3176892
## Community sizes
##   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20 
## 309 157 196  55   5   4   6   3   2   2   2   2   2   2   2   2   2   2   2   2 
##  21 
##   2
bac.module.net("SpMan",fq, abu,"Soil","Control", 2, 9)
## [1] 0.6741034
## Community sizes
##   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20 
## 101  85 157  24  16  13  12   9   6   6   5   5   5   4   4   4   3   4   3   3 
##  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40 
##   4   3   3   3   3   3   3   3   3   3   3   3   2   2   2   2   2   2   2   2 
##  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60 
##   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2 
##  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80 
##   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2 
##  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97 
##   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2
bac.module.net("SpMan",fq, abu,"Soil","Pre_flowering_drought", 2, 9)
## [1] 0.8345862
## Community sizes
##  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 
## 21 10 12  6  6 25  4  4  4  4  3  3  3  3  3  3  3  3  3  3  3  2  2  2  2  2 
## 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 
##  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2
bac.module.net("SpMan",fq, abu,"Soil","Control", 8, 18)
## [1] 0.7438389
## Community sizes
##   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20 
## 101  95 107  27  15  10  11   7   7  33   6  10   5   4   4   3   3   3   3   3 
##  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40 
##   3   3   3   3   3   3   3   2   2   2   2   2   2   2   2   2   2   2   2   2 
##  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60 
##   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2 
##  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80 
##   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2 
##  81  82  83 
##   2   2   2
bac.module.net("SpMan",fq, abu,"Soil","Pre_flowering_drought", 8, 18)
## [1] 0.6792063
## Community sizes
##   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20 
##  48 154 107 132  15   7   6   6   5   3   3   3   3   3   3   3   3   3   3   3 
##  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40 
##   3   3   3   3   3   2   3   3   2   2   2   2   2   2   2   2   2   2   2   2 
##  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60 
##   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2 
##  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76 
##   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2
bac.module.net("SpMan",fq, abu,"Leaf","Control", 2, 9)
## [1] 0.8347756
## Community sizes
##  1  2  3  4  5  6  7  8  9 10 11 12 
##  8  9  6  3  4  3  3  3  2  2  2  2
bac.module.net("SpMan",fq, abu,"Leaf","Pre_flowering_drought", 2, 9)
## [1] 0.600171
## Community sizes
##  1  2  3  4  5  6  7  8  9 10 11 12 13 
## 15 12 16 16 11  4  4  3  3  3  2  2  2
bac.module.net("SpMan",fq, abu,"Leaf","Control", 8, 18)
## [1] 0.2469185
## Community sizes
##  1  2  3  4  5  6  7  8  9 10 
## 18 33  5  5  2  8  2  2  2  2
bac.module.net("SpMan",fq, abu,"Leaf","Pre_flowering_drought", 8, 18)
## [1] 0.3142673
## Community sizes
##  1  2  3  4  5  6 
## 34 53 45  2  2  2

####################
# Module bac-fung inter###
####################

bac.fung.inter.module.net<-function(type,fq, abu, habitat, treatment, tpA, tpB){
  BF0<-read.csv("Microbiome/0000BacteriaFungi.1029x1293.13165.2019.07.10.csv", head = T, row.names = 1)
  ID.tmp<-BF0[,c("Kingdom", "Kingdom1", "Phylum",   "Class",    "Order",    "Family",   "Genus",    "Funguild", "OTU.ID",   "Morph",    "Morph1")]
  da0<-readRDS(paste0("Fig.3/BFT.",type,".sig.df.2022.05.31/",type,".Rsig.Bac-Fung.",habitat,".", treatment,".",tpA,".",tpB,".",fq,".",abu,".RDS"))
  da <- da0[da0$Kingdom != da0$Kingdom.1 ,]
  g <- graph.data.frame(da, directed=FALSE)
  fun.fc<-cluster_fast_greedy(g)
  print(modularity(fun.fc))
  modularity(fun.fc,membership(fun.fc))
  membership(fun.fc)
  print(sizes(fun.fc))###
  fun.comps <- membership(fun.fc)
  colbar <-c("#0000FF","#FF3399","#FFCC33","#9999FF", "#FF99FF", "#FFFFCC",
             "#33FFFF","#CCFF00","#CCFFCC","#FF3300","#CC99CC","#CC66CC","#FFFFCC","#CC0033","#666666","slateblue1","springgreen","steelblue1","tan1","thistle1","tomato","turquoise", "violet", "red", "yellowgreen", "peachpuff", "peru", "pink", "plum2", "purple","wheat", "cornsilk3","cornsilk","coral4","coral",
             "chocolate4","chocolate","black","chartreuse4","chartreuse","burlywood4","burlywood","brown4","blue","bisque4","bisque",
             "azure4","azure","aquamarine4","aquamarine","antiquewhite4","antiquewhite","aliceblue","dodgerblue4","dodgerblue","dimgrey",
             "deepskyblue4", "deepskyblue", "deeppink4", "deeppink","darkviolet", "darkslategray4","darkslategray",
             "darkseagreen4", "darkseagreen", "darksalmon", "darkred", "darkorchid4", "darkorchid", "darkorange4",
             "darkorange","firebrick","darkgreen", "darkgoldenrod4","grey", "darkgoldenrod", "darkcyan")
  V(g)$color <- colbar[fun.comps]
    set.seed(123)
  plot(g, edge.width=0.5,edge.color="grey", vertex.frame.color=NA,vertex.label=NA,edge.lty=1,     edge.curved=T,vertex.size=3,margin=c(0, 0,0,0)) 
}
par(mfrow=c(4,4),mar=c(0, 0, 0, 0))
bac.fung.inter.module.net("SpMan",fq, abu,"Root","Control", 2, 9)
## [1] 0.4599666
## Community sizes
##   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19 
## 174  24 155  10  17   3   3   4   5   2   4   2   2   2   2   2   2   2   2
bac.fung.inter.module.net("SpMan",fq, abu,"Root","Pre_flowering_drought", 2, 9)
## [1] 0.78
## Community sizes
## 1 2 3 4 5 6 7 
## 5 2 2 2 2 2 2
bac.fung.inter.module.net("SpMan",fq, abu,"Root","Control", 8, 18)
## [1] 0.5363242
## Community sizes
##   1   2   3   4   5   6   7   8   9  10  11 
## 109  30   8  84   3   2   2   2   2   2   2
bac.fung.inter.module.net("SpMan",fq, abu,"Root","Pre_flowering_drought", 8, 18)
## [1] 0.3662968
## Community sizes
##   1   2   3   4   5   6   7   8   9  10  11  12 
## 126  93 131  40   4   3   2   2   2   2   2   2
bac.fung.inter.module.net("SpMan",fq, abu,"Rhizosphere","Control", 2, 9)
## [1] 0.4726193
## Community sizes
##  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 
## 81 60 21 28 66 25  8  5  5  4  3  4  3  3  3  3  3  3  3  3  3  2  2  2  2  2 
## 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 
##  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2
bac.fung.inter.module.net("SpMan",fq, abu,"Rhizosphere","Pre_flowering_drought", 2, 9)
## [1] 0.7581883
## Community sizes
##  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 
## 22 10 20  7  8  6 19  4  3  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2 
## 27 28 29 30 31 
##  2  2  2  2  2
bac.fung.inter.module.net("SpMan",fq, abu,"Rhizosphere","Control", 8, 18)
## [1] 0.5350336
## Community sizes
##   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20 
## 169 157  47  11   6   6   3   3   3   2   2   2   2   2   2   2   2   2   2   2 
##  21  22  23  24  25 
##   2   2   2   2   2
bac.fung.inter.module.net("SpMan",fq, abu,"Rhizosphere","Pre_flowering_drought", 8, 18)
## [1] 0.2748164
## Community sizes
##   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19 
## 169  46  75 141   5   9   4   3   2   2   2   2   2   2   2   2   2   2   2
bac.fung.inter.module.net("SpMan",fq, abu,"Soil","Control", 2, 9)
## [1] 0.7083955
## Community sizes
##  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 
## 41  9 13  4 31 24  6  5  4  3  3  3  2  2  2  2  2  2  2  2  2  2  2  2  2  2 
## 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 
##  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2
bac.fung.inter.module.net("SpMan",fq, abu,"Soil","Pre_flowering_drought", 2, 9)
## [1] 0.8596524
## Community sizes
##  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 
## 10  9  6  4  3  3  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2
bac.fung.inter.module.net("SpMan",fq, abu,"Soil","Control", 8, 18)
## [1] 0.8165966
## Community sizes
##  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 
## 26 14 10 23  5  8 13  5  5  3  3  3  3  2  2  2  2  2  2  2  2  2  2  2  2
bac.fung.inter.module.net("SpMan",fq, abu,"Soil","Pre_flowering_drought", 8, 18)
## [1] 0.7421026
## Community sizes
##  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 
## 22 22 16 22 10  6  4  4  6  3  3  3  2  2  2  2  2  2  2  2  2  2  2  2  2  2 
## 27 28 29 30 
##  2  2  2  2
bac.fung.inter.module.net("SpMan",fq, abu,"Leaf","Control", 2, 9)
## [1] 0.4444444
## Community sizes
## 1 2 
## 3 2
bac.fung.inter.module.net("SpMan",fq, abu,"Leaf","Pre_flowering_drought", 2, 9)
## [1] 0.5
## Community sizes
## 1 2 
## 2 2
bac.fung.inter.module.net("SpMan",fq, abu,"Leaf","Control", 8, 18)
## [1] 0.5122991
## Community sizes
##  1  2  3  4 
## 21 18  2  2
bac.fung.inter.module.net("SpMan",fq, abu,"Leaf","Pre_flowering_drought", 8, 18)
## [1] 0.2612455
## Community sizes
##  1  2  3  4  5  6  7  8 
## 18 34 28 10  4  2  2  2

##########
#Plotting##
###########
library(igraph)
bac.fung.cross.net<-function(type,fq, abu, habitat, treatment, tpA, tpB){
  BF0<-read.csv("Microbiome/0000BacteriaFungi.1029x1293.13165.2019.07.10.csv", head = T, row.names = 1)
  ID.tmp<-BF0[,c("Kingdom", "Kingdom1", "Phylum",   "Class",    "Order",    "Family",   "Genus",    "Funguild", "OTU.ID",   "Morph",    "Morph1")]
  da<-readRDS(paste0("Fig.3/BFT.",type,".sig.df.2022.05.31/",type,".Rsig.Bac-Fung.",habitat,".", treatment,".",tpA,".",tpB,".",fq,".",abu,".RDS"))
  g <- graph.data.frame(da, directed=FALSE)
  g.color = droplevels(ID.tmp[ID.tmp$OTU.ID %in% V(g)$name,])
  g.color<-g.color[match(V(g)$name, g.color$OTU.ID),]
  g.color$Kingdom1 <- factor(g.color$Kingdom,  labels = c("blue", "black"))
  levels(g.color$Kingdom1) = c("blue", "black") 
  V(g)$color = as.character(g.color$Kingdom1)
  
  
  num.edges = length(E(g)) 
  num.vertices = length(V(g))
  connectance = edge_density(g,loops=FALSE)# 
  average.degree = mean(igraph::degree(g))# 
  average.path.length = average.path.length(g) 
  diameter = diameter(g, directed = FALSE, unconnected = TRUE, weights = NULL)
  edge.connectivity = edge_connectivity(g)
  clustering.coefficient = transitivity(g) 
  no.clusters = no.clusters(g)
  centralization.betweenness = centralization.betweenness(g)$centralization 
  centralization.degree = centralization.degree(g)$centralization
  fun.fc<-cluster_fast_greedy(g)
  Modularity<-modularity(fun.fc,membership(fun.fc))
  No.modules<-nrow(data.frame(sizes(fun.fc)))
  df.tmp<-data.frame(network = "BF-FF-BB",type, habitat, treatment, tpA, tpB,  num.edges, num.vertices, connectance, average.degree, average.path.length, diameter, edge.connectivity, clustering.coefficient,
                     no.clusters, centralization.betweenness,centralization.degree,  Modularity, No.modules)
  saveRDS(df.tmp, paste0("Fig.3/SpMan.network.property.2022.05.31/SpMan.network.property.BF.BB.FF.",type,".",habitat, ".", treatment,".",tpA,".",tpB,".RDS"))
  
  g.E <-data.frame(get.edgelist(g))
  names(g.E)<- c("V1", "V2")
  
  V1<-data.frame("v1"=g.E$V1)
  V2<-data.frame("v2"=g.E$V2)
  
  ID.tmpx<-ID.tmp[,c("OTU.ID", "Kingdom")]
  IDsub1<-ID.tmpx[ID.tmpx$OTU.ID %in% V1$v1, ]
  IDsub2<-ID.tmpx[ID.tmpx$OTU.ID %in% V2$v2, ]
  V1$id  <- 1:nrow(V1); V2$id  <- 1:nrow(V2)
  M1<-merge(V1, IDsub1, by.x = "v1", by.y = "OTU.ID", all.x= T); M1<-M1[order(M1$id), ]
  M2<-merge(V2, IDsub2, by.x = "v2", by.y = "OTU.ID", all.x = T); M2<-M2[order(M2$id), ]
  
  M1$BF<-"red"
  M1$BF[M1$Kingdom=="Prokaryote" & M2$Kingdom=="Prokaryote" ]<-"grey"
  M1$BF[M1$Kingdom=="Eukaryote" & M2$Kingdom=="Eukaryote" ]<-"skyblue"
  
  E(g)$color = as.character(M1$BF)
  
  set.seed(123)
  plot(g, edge.width=1,  vertex.frame.color=NA,vertex.label=NA,edge.lty=1, edge.curved=T,vertex.size=3) 
}

par(mfrow=c(4,4),mar=c(0, 0, 0, 0))
bac.fung.cross.net("SpMan",fq, abu,"Root","Control", 2, 9)
bac.fung.cross.net("SpMan",fq, abu,"Root","Pre_flowering_drought", 2, 9)
bac.fung.cross.net("SpMan",fq, abu,"Root","Control", 8, 18)
bac.fung.cross.net("SpMan",fq, abu,"Root","Pre_flowering_drought", 8, 18)

bac.fung.cross.net("SpMan",fq, abu,"Rhizosphere","Control", 2, 9)
bac.fung.cross.net("SpMan",fq, abu,"Rhizosphere","Pre_flowering_drought", 2, 9)
bac.fung.cross.net("SpMan",fq, abu,"Rhizosphere","Control", 8, 18)
bac.fung.cross.net("SpMan",fq, abu,"Rhizosphere","Pre_flowering_drought", 8, 18)

bac.fung.cross.net("SpMan",fq, abu,"Soil","Control", 2, 9)
bac.fung.cross.net("SpMan",fq, abu,"Soil","Pre_flowering_drought", 2, 9)
bac.fung.cross.net("SpMan",fq, abu,"Soil","Control", 8, 18)
bac.fung.cross.net("SpMan",fq, abu,"Soil","Pre_flowering_drought", 8, 18)

bac.fung.cross.net("SpMan",fq, abu,"Leaf","Control", 2, 9)
bac.fung.cross.net("SpMan",fq, abu,"Leaf","Pre_flowering_drought", 2, 9)
bac.fung.cross.net("SpMan",fq, abu,"Leaf","Control", 8, 18)
bac.fung.cross.net("SpMan",fq, abu,"Leaf","Pre_flowering_drought", 8, 18)

################
##Inter-bac-Fung#
################
bac.fung.inter.net<-function(type,fq, abu, habitat, treatment, tpA, tpB){
  BF0<-read.csv("Microbiome/0000BacteriaFungi.1029x1293.13165.2019.07.10.csv", head = T, row.names = 1)
  ID.tmp<-BF0[,c("Kingdom", "Kingdom1", "Phylum",   "Class",    "Order",    "Family",   "Genus",    "Funguild", "OTU.ID",   "Morph",    "Morph1")]
  ID.tmp$shape <- "circle"; ID.tmp$shape[ID.tmp$Kingdom=="Eukaryote"]<-"square"
  ID.tmp$color <- "grey"
  ID.tmp$color[ID.tmp$Funguild=="Plant pathogen"] <- "purple"
  ID.tmp$color[ID.tmp$Funguild=="SaprotrophYeast"] <- "red"
  ID.tmp$color[ID.tmp$Funguild=="Plant pathogenYeast"] <- "red"
  ID.tmp$color[ID.tmp$Funguild=="Saprotroph"] <- "brown"
  ID.tmp$color[ID.tmp$Funguild=="Arbuscular mycorrhizal"] <- "green"
  ID.tmp$color[ID.tmp$Funguild=="Endophyte"] <- "blue"
  ID.tmp$color[ID.tmp$Phylum=="Acidobacteria"] <- "blue"
  ID.tmp$color[ID.tmp$Phylum=="Actinobacteria"] <- "red"
  ID.tmp$color[ID.tmp$Phylum=="Bacteroidetes"] <- "black"
  ID.tmp$color[ID.tmp$Phylum=="Proteobacteria"] <- "green"
  ID.tmp$color[ID.tmp$Phylum=="Chloroflexi"] <- "brown"
  ID.tmp$color[ID.tmp$Phylum=="Firmicutes"] <- "yellow"
  ID.tmp$color[ID.tmp$Phylum=="Gemmatimonadetes"] <- "pink"
  ID.tmp$color[ID.tmp$Phylum=="Verrucomicrobia"] <- "purple"
  
  da0<-readRDS(paste0("Fig.3/BFT.",type,".sig.df.2022.05.31/",type,".Rsig.Bac-Fung.",habitat,".", treatment,".",tpA,".",tpB,".",fq,".",abu,".RDS"))
  da <- da0[da0$Kingdom != da0$Kingdom.1 ,]
  
  g <- graph.data.frame(da, directed=FALSE)
  g.color = droplevels(ID.tmp[ID.tmp$OTU.ID %in% V(g)$name,])
  g.color<-g.color[match(V(g)$name, g.color$OTU.ID),]
  V(g)$color = as.character(g.color$color)
  V(g)$shape <-as.character(g.color$shape)
  num.edges = length(E(g)) 
  num.vertices = length(V(g))
  connectance = edge_density(g,loops=FALSE)# 
  average.degree = mean(igraph::degree(g))# 
  average.path.length = average.path.length(g) 
  diameter = diameter(g, directed = FALSE, unconnected = TRUE, weights = NULL)
  edge.connectivity = edge_connectivity(g)
  clustering.coefficient = transitivity(g) 
  no.clusters = no.clusters(g)
  centralization.betweenness = centralization.betweenness(g)$centralization 
  centralization.degree = centralization.degree(g)$centralization
  fun.fc<-cluster_fast_greedy(g)
  Modularity<-modularity(fun.fc,membership(fun.fc))
  No.modules<-nrow(data.frame(sizes(fun.fc)))
  df.tmp<-data.frame(network = "BF",type, habitat, treatment, tpA, tpB,  num.edges, num.vertices, connectance, average.degree, average.path.length, diameter, edge.connectivity, clustering.coefficient,
                     no.clusters, centralization.betweenness,centralization.degree,  Modularity, No.modules)
  saveRDS(df.tmp, paste0("Fig.3/SpMan.network.property.2022.05.31/SpMan.network.property.BF.",type,".",habitat, ".", treatment,".",tpA,".",tpB,".RDS"))
  
  g.E <-data.frame(get.edgelist(g))
  names(g.E)<- c("V1", "V2")
  
  V1<-data.frame("v1"=g.E$V1)
  V2<-data.frame("v2"=g.E$V2)
  
  ID.tmpx<-ID.tmp[,c("OTU.ID", "Kingdom")]
  IDsub1<-ID.tmpx[ID.tmpx$OTU.ID %in% V1$v1, ]
  IDsub2<-ID.tmpx[ID.tmpx$OTU.ID %in% V2$v2, ]
  V1$id  <- 1:nrow(V1); V2$id  <- 1:nrow(V2)
  M1<-merge(V1, IDsub1, by.x = "v1", by.y = "OTU.ID", all.x= T); M1<-M1[order(M1$id), ]
  M2<-merge(V2, IDsub2, by.x = "v2", by.y = "OTU.ID", all.x = T); M2<-M2[order(M2$id), ]
  
  M1$BF<-"red"
  M1$BF[M1$Kingdom=="Prokaryote" & M2$Kingdom=="Prokaryote" ]<-"grey"
  M1$BF[M1$Kingdom=="Eukaryote" & M2$Kingdom=="Eukaryote" ]<-"skyblue"
  
  E(g)$color = as.character(M1$BF)
  
  set.seed(123)
  plot(g, edge.width=1,  vertex.frame.color=NA,vertex.label=NA,edge.lty=1, edge.curved=T,vertex.size=5) 
}

par(mfrow=c(4,4),mar=c(0, 0, 0, 0))
bac.fung.inter.net("SpMan",fq, abu,"Root","Control", 2, 9)
bac.fung.inter.net("SpMan",fq, abu,"Root","Pre_flowering_drought", 2, 9)
bac.fung.inter.net("SpMan",fq, abu,"Root","Control", 8, 18)
bac.fung.inter.net("SpMan",fq, abu,"Root","Pre_flowering_drought", 8, 18)

bac.fung.inter.net("SpMan",fq, abu,"Rhizosphere","Control", 2, 9)
bac.fung.inter.net("SpMan",fq, abu,"Rhizosphere","Pre_flowering_drought", 2, 9)
bac.fung.inter.net("SpMan",fq, abu,"Rhizosphere","Control", 8, 18)
bac.fung.inter.net("SpMan",fq, abu,"Rhizosphere","Pre_flowering_drought", 8, 18)

bac.fung.inter.net("SpMan",fq, abu,"Soil","Control", 2, 9)
bac.fung.inter.net("SpMan",fq, abu,"Soil","Pre_flowering_drought", 2, 9)
bac.fung.inter.net("SpMan",fq, abu,"Soil","Control", 8, 18)
bac.fung.inter.net("SpMan",fq, abu,"Soil","Pre_flowering_drought", 8, 18)

bac.fung.inter.net("SpMan",fq, abu,"Leaf","Control", 2, 9)
bac.fung.inter.net("SpMan",fq, abu,"Leaf","Pre_flowering_drought", 2, 9)
bac.fung.inter.net("SpMan",fq, abu,"Leaf","Control", 8, 18)
bac.fung.inter.net("SpMan",fq, abu,"Leaf","Pre_flowering_drought", 8, 18)

##################
# Fungal network #
##################
fung.intra.net<-function(type,fq, abu, habitat, treatment, tpA, tpB){
  BF0<-read.csv("Microbiome/0000BacteriaFungi.1029x1293.13165.2019.07.10.csv", head = T, row.names = 1)
  ID.tmp<-BF0[,c("Kingdom", "Kingdom1", "Phylum",   "Class",    "Order",    "Family",   "Genus",    "Funguild", "OTU.ID",   "Morph",    "Morph1")]
  ID.tmp$shape <- "circle"; ID.tmp$shape[ID.tmp$Kingdom=="Eukaryote"]<-"square"
  ID.tmp$color <- "grey"
  ID.tmp$color[ID.tmp$Funguild=="Plant pathogen"] <- "purple"
  ID.tmp$color[ID.tmp$Funguild=="SaprotrophYeast"] <- "red"
  ID.tmp$color[ID.tmp$Funguild=="Plant pathogenYeast"] <- "red"
  ID.tmp$color[ID.tmp$Funguild=="Saprotroph"] <- "brown"
  ID.tmp$color[ID.tmp$Funguild=="Arbuscular mycorrhizal"] <- "green"
  ID.tmp$color[ID.tmp$Funguild=="Endophyte"] <- "blue"
  
  da0<-readRDS(paste0("Fig.3/BFT.",type,".sig.df.2022.05.31/",type,".Rsig.Bac-Fung.",habitat,".", treatment,".",tpA,".",tpB,".",fq,".",abu,".RDS"))
  da <- da0[da0$Kingdom == "Eukaryote" & da0$Kingdom.1 == "Eukaryote",]
  
  g <- graph.data.frame(da, directed=FALSE)
  g.color = droplevels(ID.tmp[ID.tmp$OTU.ID %in% V(g)$name,])
  g.color<-g.color[match(V(g)$name, g.color$OTU.ID),]
  V(g)$color = as.character(g.color$color)
  V(g)$shape <-as.character(g.color$shape)
  num.edges = length(E(g)) 
  num.vertices = length(V(g))
  connectance = edge_density(g,loops=FALSE)# 
  average.degree = mean(igraph::degree(g))# 
  average.path.length = average.path.length(g) 
  diameter = diameter(g, directed = FALSE, unconnected = TRUE, weights = NULL)
  edge.connectivity = edge_connectivity(g)
  clustering.coefficient = transitivity(g) 
  no.clusters = no.clusters(g)
  centralization.betweenness = centralization.betweenness(g)$centralization 
  centralization.degree = centralization.degree(g)$centralization
  fun.fc<-cluster_fast_greedy(g)
  Modularity<-modularity(fun.fc,membership(fun.fc))
  No.modules<-nrow(data.frame(sizes(fun.fc)))
  df.tmp<-data.frame(network = "FF",type, habitat, treatment, tpA, tpB,  num.edges, num.vertices, connectance, average.degree, average.path.length, diameter, edge.connectivity, clustering.coefficient,
                     no.clusters, centralization.betweenness,centralization.degree,  Modularity, No.modules)
  saveRDS(df.tmp, paste0("Fig.3/SpMan.network.property.2022.05.31/SpMan.network.property.FF.",type,".",habitat, ".", treatment,".",tpA,".",tpB,".RDS"))
  
  g.E <-data.frame(get.edgelist(g))
  names(g.E)<- c("V1", "V2")
  
  V1<-data.frame("v1"=g.E$V1)
  V2<-data.frame("v2"=g.E$V2)
  
  ID.tmpx<-ID.tmp[,c("OTU.ID", "Kingdom")]
  IDsub1<-ID.tmpx[ID.tmpx$OTU.ID %in% V1$v1, ]
  IDsub2<-ID.tmpx[ID.tmpx$OTU.ID %in% V2$v2, ]
  V1$id  <- 1:nrow(V1); V2$id  <- 1:nrow(V2)
  M1<-merge(V1, IDsub1, by.x = "v1", by.y = "OTU.ID", all.x= T); M1<-M1[order(M1$id), ]
  M2<-merge(V2, IDsub2, by.x = "v2", by.y = "OTU.ID", all.x = T); M2<-M2[order(M2$id), ]
  
  M1$BF<-"red"
  M1$BF[M1$Kingdom=="Prokaryote" & M2$Kingdom=="Prokaryote" ]<-"grey"
  M1$BF[M1$Kingdom=="Eukaryote" & M2$Kingdom=="Eukaryote" ]<-"skyblue"
  
  E(g)$color = as.character(M1$BF)
  
  set.seed(123)
  plot(g, edge.width=1,  vertex.frame.color=NA,vertex.label=NA,edge.lty=1, edge.curved=T,vertex.size=5) 
}

par(mfrow=c(4,4),mar=c(0, 0, 0, 0))
fung.intra.net("SpMan",fq, abu,"Root","Control", 2, 9)
fung.intra.net("SpMan",fq, abu,"Root","Pre_flowering_drought", 2, 9)
fung.intra.net("SpMan",fq, abu,"Root","Control", 8, 18)
fung.intra.net("SpMan",fq, abu,"Root","Pre_flowering_drought", 8, 18)

fung.intra.net("SpMan",fq, abu,"Rhizosphere","Control", 2, 9)
fung.intra.net("SpMan",fq, abu,"Rhizosphere","Pre_flowering_drought", 2, 9)
fung.intra.net("SpMan",fq, abu,"Rhizosphere","Control", 8, 18)
fung.intra.net("SpMan",fq, abu,"Rhizosphere","Pre_flowering_drought", 8, 18)

fung.intra.net("SpMan",fq, abu,"Soil","Control", 2, 9)
fung.intra.net("SpMan",fq, abu,"Soil","Pre_flowering_drought", 2, 9)
fung.intra.net("SpMan",fq, abu,"Soil","Control", 8, 18)
fung.intra.net("SpMan",fq, abu,"Soil","Pre_flowering_drought", 8, 18)

fung.intra.net("SpMan",fq, abu,"Leaf","Control", 2, 9)
fung.intra.net("SpMan",fq, abu,"Leaf","Pre_flowering_drought", 2, 9)
fung.intra.net("SpMan",fq, abu,"Leaf","Control", 8, 18)
fung.intra.net("SpMan",fq, abu,"Leaf","Pre_flowering_drought", 8, 18)

####################
#Bacterial network##
####################
bac.intra.net<-function(type,fq, abu, habitat, treatment, tpA, tpB){
  BF0<-read.csv("Microbiome/0000BacteriaFungi.1029x1293.13165.2019.07.10.csv", head = T, row.names = 1)
  ID.tmp<-BF0[,c("Kingdom", "Kingdom1", "Phylum",   "Class",    "Order",    "Family",   "Genus",    "Funguild", "OTU.ID",   "Morph",    "Morph1")]
  ID.tmp$shape <- "circle"; ID.tmp$shape[ID.tmp$Kingdom=="Eukaryote"]<-"square"
  ID.tmp$color <- "grey"
  ID.tmp$color[ID.tmp$Phylum=="Acidobacteria"] <- "blue"
  ID.tmp$color[ID.tmp$Phylum=="Actinobacteria"] <- "red"
  ID.tmp$color[ID.tmp$Phylum=="Bacteroidetes"] <- "black"
  ID.tmp$color[ID.tmp$Phylum=="Proteobacteria"] <- "green"
  ID.tmp$color[ID.tmp$Phylum=="Chloroflexi"] <- "brown"
  ID.tmp$color[ID.tmp$Phylum=="Firmicutes"] <- "yellow"
  ID.tmp$color[ID.tmp$Phylum=="Gemmatimonadetes"] <- "pink"
  ID.tmp$color[ID.tmp$Phylum=="Verrucomicrobia"] <- "purple"
  
  da0<-readRDS(paste0("Fig.3/BFT.",type,".sig.df.2022.05.31/",type,".Rsig.Bac-Fung.",habitat,".", treatment,".",tpA,".",tpB,".",fq,".",abu,".RDS"))
  da <- da0[da0$Kingdom == "Prokaryote" & da0$Kingdom.1 == "Prokaryote",]
  
  g <- graph.data.frame(da, directed=FALSE)
  g.color = droplevels(ID.tmp[ID.tmp$OTU.ID %in% V(g)$name,])
  g.color<-g.color[match(V(g)$name, g.color$OTU.ID),]
  V(g)$color = as.character(g.color$color)
  V(g)$shape <-as.character(g.color$shape)
  
  num.edges = length(E(g)) 
  num.vertices = length(V(g))
  connectance = edge_density(g,loops=FALSE)# 
  average.degree = mean(igraph::degree(g))# 
  average.path.length = average.path.length(g) 
  diameter = diameter(g, directed = FALSE, unconnected = TRUE, weights = NULL)
  edge.connectivity = edge_connectivity(g)
  clustering.coefficient = transitivity(g) 
  no.clusters = no.clusters(g)
  centralization.betweenness = centralization.betweenness(g)$centralization 
  centralization.degree = centralization.degree(g)$centralization
  fun.fc<-cluster_fast_greedy(g)
  Modularity<-modularity(fun.fc,membership(fun.fc))
  No.modules<-nrow(data.frame(sizes(fun.fc)))
  df.tmp<-data.frame(network = "BB",type, habitat, treatment, tpA, tpB,  num.edges, num.vertices, connectance, average.degree, average.path.length, diameter, edge.connectivity, clustering.coefficient,
                     no.clusters, centralization.betweenness,centralization.degree,  Modularity, No.modules)
  saveRDS(df.tmp, paste0("Fig.3/SpMan.network.property.2022.05.31/SpMan.network.property.BB.",type,".",habitat, ".", treatment,".",tpA,".",tpB,".RDS"))
  
  g.E <-data.frame(get.edgelist(g))
  names(g.E)<- c("V1", "V2")
  
  V1<-data.frame("v1"=g.E$V1)
  V2<-data.frame("v2"=g.E$V2)
  
  ID.tmpx<-ID.tmp[,c("OTU.ID", "Kingdom")]
  IDsub1<-ID.tmpx[ID.tmpx$OTU.ID %in% V1$v1, ]
  IDsub2<-ID.tmpx[ID.tmpx$OTU.ID %in% V2$v2, ]
  V1$id  <- 1:nrow(V1); V2$id  <- 1:nrow(V2)
  M1<-merge(V1, IDsub1, by.x = "v1", by.y = "OTU.ID", all.x= T); M1<-M1[order(M1$id), ]
  M2<-merge(V2, IDsub2, by.x = "v2", by.y = "OTU.ID", all.x = T); M2<-M2[order(M2$id), ]
  
  M1$BF<-"red"
  M1$BF[M1$Kingdom=="Prokaryote" & M2$Kingdom=="Prokaryote" ]<-"grey"
  M1$BF[M1$Kingdom=="Eukaryote" & M2$Kingdom=="Eukaryote" ]<-"skyblue"
  
  E(g)$color = as.character(M1$BF)
  
  set.seed(123)
  plot(g, edge.width=1,  vertex.frame.color=NA,vertex.label=NA,edge.lty=1, edge.curved=T,vertex.size=5) 
}

par(mfrow=c(4,4),mar=c(0, 0, 0, 0))
bac.intra.net("SpMan",fq, abu,"Root","Control", 2, 9)
bac.intra.net("SpMan",fq, abu,"Root","Pre_flowering_drought", 2, 9)
bac.intra.net("SpMan",fq, abu,"Root","Control", 8, 18)
bac.intra.net("SpMan",fq, abu,"Root","Pre_flowering_drought", 8, 18)

bac.intra.net("SpMan",fq, abu,"Rhizosphere","Control", 2, 9)
bac.intra.net("SpMan",fq, abu,"Rhizosphere","Pre_flowering_drought", 2, 9)
bac.intra.net("SpMan",fq, abu,"Rhizosphere","Control", 8, 18)
bac.intra.net("SpMan",fq, abu,"Rhizosphere","Pre_flowering_drought", 8, 18)

bac.intra.net("SpMan",fq, abu,"Soil","Control", 2, 9)
bac.intra.net("SpMan",fq, abu,"Soil","Pre_flowering_drought", 2, 9)
bac.intra.net("SpMan",fq, abu,"Soil","Control", 8, 18)
bac.intra.net("SpMan",fq, abu,"Soil","Pre_flowering_drought", 8, 18)

bac.intra.net("SpMan",fq, abu,"Leaf","Control", 2, 9)
bac.intra.net("SpMan",fq, abu,"Leaf","Pre_flowering_drought", 2, 9)
bac.intra.net("SpMan",fq, abu,"Leaf","Control", 8, 18)
bac.intra.net("SpMan",fq, abu,"Leaf","Pre_flowering_drought", 8, 18)

par(mfrow=c(2,2),mar=c(0, 0, 0, 0))
fung.intra.net("SpMan",fq, abu,"Rhizosphere","Control", 2, 9)
fung.intra.net("SpMan",fq, abu,"Rhizosphere","Pre_flowering_drought", 2, 9)
bac.intra.net("SpMan",fq, abu,"Leaf","Control", 2, 9)
bac.intra.net("SpMan",fq, abu,"Leaf","Pre_flowering_drought", 2, 9)

library(tidyverse) 
type = "SpMan"
mydir = paste0("Fig.3/BFT.",type,".sig.df.2022.05.31/")
da <- list.files(path=mydir, full.names=TRUE) %>% map_dfr(readRDS)
BF0<-read.csv("Microbiome/0000BacteriaFungi.1029x1293.13165.2019.07.10.csv", head = T, row.names = 1)
ID.tmp<-BF0[,c("Kingdom",   "Kingdom1", "Phylum",   "Class",    "Order",    "Family",   "Genus",    "Funguild", "OTU.ID",   "Morph",    "Morph1")]
da.fung <- droplevels(da[da$Kingdom == "Eukaryote" & da$Kingdom.1 == "Eukaryote" & da$Habitat == "Rhizosphere" & da$TPA ==2,])
da.fung$interaction<-"Inter-Guilds"
da.fung$interaction[da.fung$Funguild==da.fung$Funguild.1]<-"Intra-Guild"
da.fung$ab<-1

lev<-interaction(da.fung$interaction, da.fung$Treatment, da.fung$TPA, da.fung$Habitat,sep = ":") ## ccombining factors for Barplot profiling
da1<-aggregate(da.fung$ab,by=list(lev) , sum) # generate the mean for each factor level
library(splitstackshape)
dax.fung<-cSplit(da1, "Group.1", ":")
names(dax.fung)<-c("abu", "interaction","Treatment",  "tpA", "Habitat")
dax.fung$Treatment<- factor(dax.fung$Treatment, labels=c("Control", "Drought"))
g1 <- ggplot(dax.fung, aes(x = Treatment, y = abu, fill= interaction)) +
  geom_bar(stat='identity', position = "fill")+
  theme_bw()+xlab("")+ylab("Proportion")+
  scale_fill_manual(values = c("purple", "pink"))+
  ggtitle("Rhizosphere: FF")+
  theme(plot.title = element_text(size = 12,face="bold",hjust=0.5),
        strip.text = element_text(size = 10,face="bold"),
        legend.title = element_blank(),
        legend.text = element_text(colour="black", size=10, face="bold"),
        axis.text.y=element_text(size=10,colour="black",face="bold"),
        axis.text.x=element_text(size=10,colour="black",face="bold",angle = 45),
        axis.title=element_text(size=10,colour="black",face="bold"))+
  scale_y_continuous(labels = scales::percent)

da.bac <- da[da$Kingdom == "Prokaryote" & da$Kingdom.1 == "Prokaryote" & da$Habitat == "Leaf" & da$TPA ==2,]
da.bac$interaction<-"Inter-Phyla"
da.bac$interaction[da.bac$Phylum==da.bac$Phylum.1]<-"Intra-Phylum"
da.bac$ab<-1

lev<-interaction(da.bac$interaction, da.bac$Treatment, da.bac$TPA, da.bac$Habitat,sep = ":") ## ccombining factors for Barplot profiling
da1<-aggregate(da.bac$ab,by=list(lev) , sum) # generate the mean for each factor level
library(splitstackshape)
dax.bac<-cSplit(da1, "Group.1", ":")
names(dax.bac)<-c("abu", "interaction","Treatment",  "tpA", "Habitat")
dax.bac$Treatment<- factor(dax.bac$Treatment, labels=c("Control", "Drought"))
g2 <- ggplot(dax.bac, aes(x = Treatment, y = abu, fill= interaction)) +
  geom_bar(stat='identity', position = "fill")+
  theme_bw()+xlab("")+ylab("Proportion")+
  scale_fill_manual(values = c("purple", "pink"))+
  ggtitle("Leaf: BB")+
  theme(plot.title = element_text(size = 12,face="bold",hjust=0.5),
        strip.text = element_text(size = 10,face="bold"),
        legend.title = element_blank(),
        legend.text = element_text(colour="black", size=10, face="bold"),
        axis.text.y=element_text(size=10,colour="black",face="bold"),
        axis.text.x=element_text(size=10,colour="black",face="bold",angle = 45),
        axis.title=element_text(size=10,colour="black",face="bold"))+
  scale_y_continuous(labels = scales::percent)

library(gridExtra)
grid.arrange(g1,g2)